package com.jvm;

import com.sun.crypto.provider.DESKeyFactory;

public class TestJDKClassLoader {

  /*  public TestJDKClassLoader(URLClassPath ucp, AccessControlContext acc) {
        this.ucp = ucp;
        this.acc = acc;
    }*/

    public TestJDKClassLoader() {
    }

    static class MyClassLoad extends ClassLoader{
        private String classPath;

        public MyClassLoad(ClassLoader parent, String classPath) {
            super(parent);
            this.classPath = classPath;
        }

        public MyClassLoad(String s) {
           this.classPath = s;
        }

       /* private byte[] loadByte(String name) throws Exception{
            name = name.replace('.', '/').concat(".class");
            FileInputStream fis = new FileInputStream(classPath + "/" + name);
            int len = fis.available();
            byte[] bytes = new byte[len];
            fis.read(bytes);
            fis.close();
            return bytes;
        }
*/
       /* protected Class<?> findClass(String name) throws ClassNotFoundException{
         try {
             byte[] data  = loadByte(name);
             return defineClass(name,data,0,data.length);
            }catch (Exception e){
               e.printStackTrace();
               throw new ClassNotFoundException();
            }
        }*/

        /**
         * 重写类加载器
         * @param args
         * @throws Exception
         */
        /*protected Class<?> loadClass(String name, boolean resolve)
                throws ClassNotFoundException
        {
            synchronized (getClassLoadingLock(name)) {
                // First, check if the class has already been loaded
                Class<?> c = findLoadedClass(name);
                    if (c == null) {
                        // If still not found, then invoke findClass in order
                        // to find the class.
                        long t1 = System.nanoTime();
                        c = findClass(name);

                        // this is the defining class loader; record the stats
                        //sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                        sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                        sun.misc.PerfCounter.getFindClasses().increment();
                    }
                if (resolve) {
                    resolveClass(c);
                }
                return c;
            }
        }*/

    }
  /*  private final URLClassPath ucp;

    *//* The context to be used when loading classes and resources *//*
    private final AccessControlContext acc;
    protected Class<?> findClass(final String name)
            throws ClassNotFoundException
    {
        final Class<?> result;
        try {
            result = AccessController.doPrivileged(
                    new PrivilegedExceptionAction<Class<?>>() {
                        public Class<?> run() throws ClassNotFoundException {
                            String path = name.replace('.', '/').concat(".class");
                            Resource res = ucp.getResource(path, false);
                            if (res != null) {
                                try {
                                    return null;
                                } catch (Exception e) {
                                    throw new ClassNotFoundException(name, e);
                                }
                            } else {
                                return null;
                            }
                        }
                    }, acc);
        } catch (java.security.PrivilegedActionException pae) {
            throw (ClassNotFoundException) pae.getException();
        }
        if (result == null) {
            throw new ClassNotFoundException(name);
        }
        return result;
    }*/


    /**
     *
     */
    public void test(){
        System.out.println(String.class.getClassLoader());
        System.out.println(DESKeyFactory.class.getClassLoader().getClass().getName());
        System.out.println(TestJDKClassLoader.class.getClassLoader().getClass().getName());
        System.out.println();
        ClassLoader appClassLoader = ClassLoader.getSystemClassLoader();
        ClassLoader extClassLoader = appClassLoader.getParent();
        ClassLoader bootstrapLoader = extClassLoader.getParent();
        System.out.println("the bootstrapLoader:"+bootstrapLoader);
        System.out.println("the extClassLoader:"+extClassLoader);
        System.out.println("the appClassLoader:"+appClassLoader);

        System.out.println();
        //
        System.out.println("extClassloader加载以下文件：");
        System.out.println(System.getProperty("java.class.path"));
        System.out.println();
        System.out.println("appClassLoader加载以下文件：");
        System.out.println(System.getProperty("java.class.path"));
        //AppletClassLoader

    }

    public static void main(String[] args) throws Exception {
        MyClassLoad myClassLoad = new MyClassLoad("D:/data1");
        //Class aClass = myClassLoad.loadClass("com.jvm.User");
       /* Class aClass = myClassLoad.loadClass("java.lang.String");
        Object obj = aClass.newInstance();
        Method syso = aClass.getDeclaredMethod("syso", null);
        syso.invoke(obj,null);
        System.out.println(aClass.getClassLoader().getClass().getName());

        System.out.println(String.class.getClassLoader());
        System.out.println(com.sun.crypto.provider.DESKeyFactory.class.getClassLoader().getClass().getName());
        System.out.println(TestJDKClassLoader.class.getClassLoader().getClass().getName());
        System.out.println(ClassLoader.getSystemClassLoader().getClass().getName());*/
        //URLClassLoader.newInstance()
        System.out.println("输出测试： ");
        new TestJDKClassLoader().test();
    }
}
